package com.ruoyi.system.model.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.google.gson.Gson;
import com.qiniu.common.Constants;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.enums.Constant;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ImgUntil;
import com.ruoyi.common.utils.ImgUntilYS;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Objects;

import static com.fasterxml.jackson.databind.type.LogicalType.DateTime;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/28 11:07
 */
@Service
public class UploadService {

    @Autowired
    private ISysDictDataService dictDataService;

    private String accessKey;
    private String secretKey;
    private String bucket;
    private String domain;


    String endpoint;
    // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
    String accessKeyId;
    String accessKeySecret ;
    String bucketName;
    String folder;


    private void config(String type){
        if (Objects.equals(Constant.ALIYUN,type)){
            SysDictData sysDictData = new SysDictData();
//        sysDictData.setDictType("qiniuyun");
            sysDictData.setDictType(type);
            List<SysDictData> sysDictDataList = dictDataService.selectDictDataList(sysDictData);
            for (SysDictData dictData : sysDictDataList) {
                if ("endpoint".equals(dictData.getDictLabel())){
                    endpoint = dictData.getDictValue();
                }
                if ("ACCESS_KEY".equals(dictData.getDictLabel())){
                    accessKeyId = dictData.getDictValue();
                }else if ("SECRET_KEY".equals(dictData.getDictLabel())){
                    accessKeySecret = dictData.getDictValue();
                }else if ("bucket".equals(dictData.getDictLabel())){
                    bucketName = dictData.getDictValue();
                }else if ("folder".equals(dictData.getDictLabel())){
                    folder = dictData.getDictValue();
                }
            }
        }else {
            SysDictData sysDictData = new SysDictData();
//        sysDictData.setDictType("qiniuyun");
            sysDictData.setDictType(type);
            List<SysDictData> sysDictDataList = dictDataService.selectDictDataList(sysDictData);
            for (SysDictData dictData : sysDictDataList) {
                if ("domain".equals(dictData.getDictLabel())){
                    domain = dictData.getDictValue();
                }
                if ("ACCESS_KEY".equals(dictData.getDictLabel())){
                    accessKey = dictData.getDictValue();
                }else if ("SECRET_KEY".equals(dictData.getDictLabel())){
                    secretKey = dictData.getDictValue();
                }else if ("bucket".equals(dictData.getDictLabel())){
                    bucket = dictData.getDictValue();
                }
            }
        }
    }

    //上传文件
    public String upload2Qiniu(String filePath,String fileName){
        config(Constant.QINIUQ);
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Region.huanan());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        String result = null;
        try {
            Response response = uploadManager.put(filePath, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet =
                    new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(domain + putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return null;
    }

    //上传文件
    public  String upload2Qiniu(byte[] bytes, String fileName){

        bytes = ImgUntilYS.compressPicForScale(bytes, 2048l);


        config(Constant.QINIUQ);
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Region.huanan());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(bytes, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet =
                    new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println( domain + putRet.key);
            return  domain + putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return null;
    }
    public  String uploadApp2Qiniu(byte[] bytes, String fileName){


        config(Constant.QINIUQ);
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Region.huanan());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(bytes, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet =
                    new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println( domain + putRet.key);
            return  domain + putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return null;
    }

    public String upload2aliyun(MultipartFile file, String generate) {

        //LTAI4Fz5S8QLbyB92ZeyHFwz
        //kAzZh0hbiX2hNxpF8ZqD6hDyNzb0uv

        config(Constant.ALIYUN);
        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
            InputStream inputStream = file.getInputStream();
            //获取文件真实名称
            String originalFilename = file.getOriginalFilename();
            //重命名，防止相同文件出现覆盖
            //生成的f4f2e1a3-391a-4d5a-9438-0c9f5d27708c 需要替换成 f4f2e1a3391a4d5a94380c9f5d27708c
            //新的文件名
            originalFilename = generate + originalFilename;
            //2、把文件按照日期进行分类
            String datePath = DateUtils.dateTimeNow("yyyy/MM/dd");
            //拼接
            originalFilename = folder + "/" + datePath + "/" + originalFilename;
            // oss实现上传文件
            ossClient.putObject(bucketName, originalFilename, inputStream);
            // 关闭OSSClient
            ossClient.shutdown();
            //把上传之后文件路径返回,手动拼接出来
            String url = "https://" + bucketName + "." + endpoint + "/" + originalFilename;
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
